package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.PurcartBean;
import com.lovo.stock.bean.SacartBean;
import com.lovo.stock.bean.UserBean;
import com.lovo.stock.mapper.SacartMapper;
import com.lovo.stock.service.ISacartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SacartServiceImpl extends ServiceImpl<SacartMapper, SacartBean> implements ISacartService {
    @Autowired
    SacartMapper sacartMapper;

    @Override
    public List<SacartBean> querySacart(int userId) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("sacart_user_id",userId);

        return sacartMapper.selectList(q);
    }

    @Override
    public void addSacart(SacartBean sacartBean) {
        //首先判断购物车内有无该商品
        QueryWrapper q=new QueryWrapper<>();
        q.eq("sacart_good_num",sacartBean.getSacartGoodNum());
        SacartBean sacart=sacartMapper.selectOne(q);
        //如果购物车没有该商品,则加入购物车
        if (sacart==null){
            sacartMapper.insert(sacartBean);
        }else {
            //购物车有该商品,采购数加采购数
            sacart.setSacartNumber(sacart.getSacartNumber()+1);
            sacartMapper.updateById(sacart);
        }
    }

    @Override
    public void deleteSacart(int id) {
        sacartMapper.deleteById(id);
    }

    @Override
    public void deleteSacartByUserId(UserBean userBean) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("sacart_user_id",userBean.getUserId());
        sacartMapper.delete(q);
    }

    @Override
    public void updateSacart(SacartBean sacartBean) {
        sacartMapper.updateById(sacartBean);
    }
}
